package com.application.TextNode;

import java.io.File;
import java.util.ArrayList;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import ReadWriteXML.DOMUltil;
import ReadWriteXML.WriteXML;
import android.graphics.Canvas;
import android.util.Log;

public class TextNode extends INodeText{
	ArrayList<INodeText> nodes = new ArrayList<INodeText>();

	public TextNode(){
		
	}
	public TextNode(long id, String title, String text, float x, float y, String user) {
		super(id, title, text, x, y, user);

	}

	@Override
	public void add(INodeText node) {
		node.setDepth(this.depth + 1);
		nodes.add(node);
	}

	@Override
	public void remove(INodeText node) {

		for (INodeText i : nodes) {
			i.remove(node);
		}
		nodes.remove(node);
	}

	@Override
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int size = nodes.size();
		float d,h,sin, alpha;
		for (INodeText node : nodes) {
			node.onDraw(canvas);
		}
	}
	
	//An
	public void autoArrangeTextNode() {
		Log.d("An","text: " + this.text);
		int size = this.nodes.size();
		Log.d("An","node size: " + size);
		INodeText cl = null;
		for (int i = 0; i < size; i++) {
			int z = 0;
			if (i > 0)
				z = nodes.get(i - 1).getSumChildren()+ 1;
			Log.d("An","z: " + z);
			 if(z==0)
				 z=1;
			if (i > 0) {
				Log.d("An","i > 0");
				nodes.get(i).setX(this.x + 30);
				nodes.get(i).setY(nodes.get(i - 1).y + z * (this.heigh + 15));
			} else {
				Log.d("An","i = 0");
				nodes.get(i).setX(this.x + 30);
				nodes.get(i).setY(this.y + this.heigh + 15);
			}
			nodes.get(i).autoArrangeTextNode();

		}
	}
	//end
	
	@Override
	public INodeText getById(long id) {
		INodeText rs = null;
		if (this.id == id)
			rs = this;
		else {
			for (INodeText node : nodes) {
				rs = node.getById(id);
				if (rs != null)
					;
				return rs;
			}
		}
		return null;
	}

	@Override
	public ArrayList<INodeText> getChildren() {
		return nodes;
	}

	public INodeText getCollision(float x, float y) {
		INodeText rs = null;
		if (this.isCollision(x, y))
			return this;
		for (INodeText node : nodes) {
			rs = node.getCollision(x, y);
			if (rs != null)
				return rs;
		}
		return null;
	}

	public void setPostion(float x, float y) {
		super.setPostion(x, y);
	}

	public void moveAll(float moveX, float moveY) {
		super.setPostion(this.x + moveX, this.y + moveY);
		for (INodeText node : nodes) {
			node.moveAll(moveX, moveY);// (node.x+moveX, node.y+moveY);
		}
	}
	
	@Override
	public int getSumChildren() {
		int rs=this.nodes.size();
		for(INodeText node:this.nodes){
			rs+=node.getSumChildren();
		}
		return rs;
	}

	@Override
	public int getNodeHeight() {
		int rs=0;
		if(this.nodes.size()>0)
			rs++;
		return 0;
	}
	
	@Override
	public Element toXmlText(Document doc) {
		Element e=doc.createElement("TreeNode");
		e.setAttribute("ID", this.id+"");
		e.setAttribute("title", this.title);
		e.setAttribute("text", this.text+"");
		e.setAttribute("x", this.x+"");
		e.setAttribute("y", this.y+"");
		e.setAttribute("user",this.user);
		for(INodeText node:this.nodes){
			e.appendChild(node.toXmlText(doc));
		}		
		return e;
	}
	public void writeXML(String path){
		File f = new File(path);
		if(f.exists())
			f.delete();
		Document doc = null;;
		doc = WriteXML.insertRoot(doc, path, "Root");
		doc.getFirstChild().appendChild(this.toXmlText(doc));
		DOMUltil.writeXmlToFile(path, doc);
		
	}

	@Override
	public INodeText readXML(Element element) {
		this.id=Long.parseLong(element.getAttribute("ID"));
		this.title=element.getAttribute("title");
		this.text=element.getAttribute("text");
		this.x=Float.parseFloat(element.getAttribute("x"));
		this.y=Float.parseFloat(element.getAttribute("y"));
		this.user=element.getAttribute("user");
		NodeList nodeList=element.getChildNodes();
		for( int i=0;i<nodeList.getLength();i++){
			TextNode n=new TextNode();
			this.add(n.readXML((Element) nodeList.item(i)));
		}
		return this;
	}
	public boolean clearChildren(){
		for(INodeText node:this.nodes){
			node.clearChildren();
			this.remove(node);
		}
		return true;
	}
}
